import 'dart:async';
import 'package:flutter_aliplayer/flutter_aliplayer.dart';
import 'package:flutter_aliplayer/flutter_aliplayer_factory.dart';
import 'package:flutter_livepush_plugin/base/live_base.dart';
import 'package:flutter_livepush_plugin/base/live_base_def.dart';
import 'package:flutter_livepush_plugin/pusher/live_push_config.dart';
import 'package:flutter_livepush_plugin/pusher/live_push_def.dart';
import 'package:flutter_livepush_plugin/pusher/live_pusher.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/live/qkd_player_logic.dart';
import 'package:sports_new/controller/record/player_logic.dart';
import 'package:sports_new/entity/live/live_detail_entity.dart';
import 'package:sports_new/entity/live/expert_room_entity.dart';
import 'package:sports_new/server/api/live_api.dart';
import 'package:sports_new/server/api/other_api.dart';
import 'package:sports_new/utils/image_utils.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/view/live/items/qkd_player.dart';
import 'package:sports_new/widgets/base/qkd_dialog.dart';

import '../../view/overlay/live_float_widget.dart';

class LiveNormalLogic extends GetxController{
  int? liveId = Get.arguments;
  LiveDetailEntity? liveDetail;
  late QkdPlayerLogic playerLogic;
  Timer? _timer;
  bool _onPush = false;
  bool _mirror = false;
  bool _clickStart = false;
  int _count = 3;
  bool waitStart = false;
  bool hasInit = false;
  String? shareUrl;

  bool get onPush => _onPush;
  int get count => _count;
  bool get mirror => _mirror;
  bool get clickStart => _clickStart;

  set clickStart(bool value) {
    _clickStart = value;
    update();
  }
  set mirror(bool value) {
    _mirror = value;
    update();
  }
  set count(int value) {
    _count = value;
    update();
  }
  set onPush(bool value) {
    _onPush = value;
    update();
  }

  @override
  void onInit() async{
    liveDetail = await LiveApi.getLiveDetail(liveId);
    if(liveDetail?.status == null || liveDetail?.status == 0) waitStart = true;
    playerLogic = Get.put(QkdPlayerLogic(),tag: "${Get.arguments}");
    playerLogic.url = liveDetail?.url??"";
    if(liveDetail?.landscapeScreen == 0){
      playerLogic.setFit(false);
    }
    LiveFloatController.instance.prepare(liveDetail);
    hasInit = false;
    update();
    _timer = Timer.periodic(
      const Duration(seconds: 5),
      (timer) => getData()
    );
    super.onInit();
  }

  Future getData() async{
    var data = await LiveApi.getLiveDetail(liveId);
    if(data?.hot.hasValue??false) {
      liveDetail?.hot = data?.hot;
    }
    if(waitStart && data?.status != null && data?.status != 0){
      liveDetail = data;
      waitStart = false;
      playerLogic.doRefresh();
    }
    if(liveDetail?.isFocus != data?.isFocus){
      liveDetail?.isFocus = data?.isFocus;
    }
    update();
  }

  Future getResource() async{
    var e = await OtherApi.getAppList("app_page_share");
    if(e != null) shareUrl = e.first.content;
    update();
  }

  Future exit() async{
    Utils.onEvent('spzb',params: {'spzb':4});
    Get.back();
  }

  @override
  void onClose() {
    _timer?.cancel();
    playerLogic.dispose();
    super.onClose();
  }
}